<?php 
//商品尺寸和颜色属性id
define('GOODS_SIZE_ATTRIBUTE_ID', 211);
define('GOODS_COLOR_ATTRIBUTE_ID', 212);
define('GOODS_TAOBAO_URL_ATTRIBUTE_ID', 213);


function get_sub_cat($cat_id = array()) 
{
    static $res = NULL;

	$parent_id = implode(",", $cat_id);

    if ($res === NULL)
    {
        $data = read_static_cache("sub_cat_$parent_id");
        if ($data === false)
        {
            $sql = "SELECT cat_id, cat_name, parent_id ".
				" FROM " . $GLOBALS['ecs']->table('category') .
				" WHERE ". " parent_id IN ($parent_id) AND is_show = '1' ".
				" ORDER BY sort_order ASC";
            $res = $GLOBALS['db']->getAll($sql);
            write_static_cache("sub_cat_$parent_id", $res);
        }
        else
        {
            $res = $data;
        }
    }

    if (!$res)
    {
        return 0;
    }
	$arr = array();
	foreach($res as $row) {
		$arr[$row["parent_id"]][] = array("cat_id"=>$row["cat_id"], "cat_name"=>$row["cat_name"]);
	}
	return $arr;
}
function get_help_articles() {	
	static $arr = null;
	if ($arr !== null) return $arr;
	$arr = read_static_cache("help_articles");
	if ($arr === false) { 
	
		$sql = "SELECT * FROM ". $GLOBALS['ecs']->table('article_cat'). 
				" WHERE parent_id = '". HELP_ARTICLE_CAT_ID ."' ORDER BY `sort_order` ASC";
		$res = $GLOBALS['db']->getAll($sql);

		foreach ($res as $row) {
			$arr[$row["cat_id"]] = array("cat_name"=>$row["cat_name"]);
			$arr_cat_id[] = $row["cat_id"];
		}

		$arr_cat_id[] = HELP_ARTICLE_CAT_ID ;
		$sql = "SELECT article_id, cat_id, title ".  
				" FROM ". $GLOBALS['ecs']->table('article'). 
				" WHERE cat_id IN (". implode(",", $arr_cat_id) .")" .
				" AND is_open = 1";

		$res = $GLOBALS['db']->getAll($sql);
		

		foreach ($res as $row) {
			$arr[$row["cat_id"]]["articles"][] = $row;
		}
		write_static_cache("help_articles", $arr);
	}
	
	return $arr;
}
/**
 * 获得指定的文章的详细信息
 *
 * @access  private
 * @param   integer     $article_id
 * @return  array
 */
function get_article_info2($article_id)
{
    /* 获得文章的信息 */
    $sql = "SELECT * ".
            "FROM " .$GLOBALS['ecs']->table('article'). 
            " WHERE article_id = '$article_id'";
    $row = $GLOBALS['db']->getRow($sql);

    if ($row !== false)
    {
        $row['comment_rank'] = ceil($row['comment_rank']);                              // 用户评论级别取整
        $row['add_time']     = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']); // 修正添加时间显示

        /* 作者信息如果为空，则用网站名称替换 */
        if (empty($row['author']) || $row['author'] == '_SHOPHELP')
        {
            $row['author'] = $GLOBALS['_CFG']['shop_name'];
        }
    }
    return $row;
}
/**
 * 获取广告信息
 * return: 
 * Array ( 
 * 	[0] => Array ( 
 * 		[ad_id] => 14 
 * 		[position_id] => 4 
 * 		[media_type] => 0 
 * 		[ad_link] => http://phpor.net/ 
 * 		[ad_code] => 1314412842113814575.jpg 
 * 		[ad_name] => 我是左侧广告 
 * 		[ad_width] => 160 
 * 		[ad_height] => 160 
 * 		[position_style] => {foreach from=$ads item=ad} {/foreach}{$ad}
 *		) 
 *	) 
 */
function get_ads_list($pos_id, $number=10, $sort='DESC', $type='array') 
{
	$data = read_static_cache("get_ads_list_$pos_id");
	if ($data === false){
		$time = gmtime();
		$sort = strtolower($sort) === 'desc' ? 'DESC' : 'ASC';
		$sql  = 'SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, p.ad_height, p.position_style ' . 
			'FROM ' . $GLOBALS['ecs']->table('ad') . ' AS a '.
			'LEFT JOIN ' . $GLOBALS['ecs']->table('ad_position') . ' AS p ON a.position_id = p.position_id ' .
			"WHERE enabled = 1 AND start_time <= '" . $time . "' AND end_time >= '" . $time . "' ".
			"AND a.position_id = '" . $pos_id . "' " .
			'ORDER BY a.ad_id DESC LIMIT ' . $number;
		$res = $GLOBALS['db']->GetAll($sql);
		write_static_cache("get_ads_list_$pos_id", $res);
	} else {
		$res = $data;
	}
	foreach($res as &$item) {
		$item["src"] = (strpos($row['ad_code'], 'http://') === false && strpos($row['ad_code'], 'https://') === false) ?
			DATA_DIR . "/afficheimg/{$item['ad_code']}" : $item['ad_code'];
	}
	return $res;
}

function get_my_message() {
	$sql = "SELECT article_id,title,add_time,description,article_type,author ".
		" FROM ". $GLOBALS['ecs']->table('article') . 
		" WHERE cat_id = '".MY_MESSAGE_ID."'" ;
	$res = $GLOBALS['db']->GetAll($sql);
	if (!$res) return array();
	foreach( $res as &$row) {
        $row['comment_rank'] = ceil($row['comment_rank']);                              // 用户评论级别取整
        $row['add_time']     = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']); // 修正添加时间显示

        /* 作者信息如果为空，则用网站名称替换 */
        if (empty($row['author']) || $row['author'] == '_SHOPHELP') {
            $row['author'] = $GLOBALS['_CFG']['shop_name'];
        }
	}
	return $res;
}

function get_cat_goods_count() {
	$flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;

	/* 检查购物车中是否有商品 */
	$sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('cart') .
		" WHERE session_id = '" . SESS_ID . "' " .
		"AND parent_id = 0 AND is_gift = 0 AND rec_type = '$flow_type'";

    return $GLOBALS['db']->getOne($sql);
}

function get_userinfo($user_id) {
	$sql = "SELECT * FROM ". $GLOBALS['ecs']->table('users') .
		" WHERE user_id = '$user_id' ";

    return $GLOBALS['db']->getRow($sql);
}
function get_user_order_count($user_id) {
	$sql = "SELECT count(*) FROM ". $GLOBALS['ecs']->table('order_info') .
		" WHERE user_id = '$user_id' AND add_time > " . (time() -  86400 *30) .
		" limit 5";

    return $GLOBALS['db']->getAll($sql);
}

/**
 * 获取最新特价商品
 * @param $cnt
 */
function get_offprice_goods($cnt) {
	$sql = "SELECT * FROM ". $GLOBALS['ecs']->table('goods') .
		" WHERE " .
		" shop_price < market_price ".
		" AND is_hot > 0 " .
		" ORDER BY add_time desc " .
		" LIMIT $cnt";

    return $GLOBALS['db']->getAll($sql);	
}

function get_user_unread_message_count($user_id) {
	$sql = "SELECT count(*) FROM ". $GLOBALS['ecs']->table('admin_message') .
		" WHERE receiver_id = '$user_id' " .
		" AND readed = 0 ";

    return $GLOBALS['db']->getOne($sql);	
	
}

function get_user_undeal_order_count($user_id) {
	$sql = "SELECT count(*) FROM ". $GLOBALS['ecs']->table('order_info') .
		" WHERE user_id = '$user_id' " .
		" AND pay_status =  " . PS_UNPAYED .
		" AND order_status in ( " . OS_UNCONFIRMED ."," . OS_CONFIRMED .")";

    return $GLOBALS['db']->getOne($sql);		
}
function get_support_banks() {
	return array(
		1	=> '中国农行银行',
		2	=> '中国建设银行',
		3	=> '招行银行',
		4	=> '交通银行',
		5	=> '中国银行',
		6	=> '中信银行',
		7	=> '广东发展银行',
		8	=> '民生银行',
		9	=> '浦发银行',
		10	=> '兴业银行',
	);
}
function get_unpurchase_type() {
	return array(
		1	=> '银行账号',
		2	=> '支付宝账号',
		3	=> '快钱账号',
	);
}
function _get_order_status() {
	return array(
		'0'	=> '未确认',
		'1'	=> '已支付',
		'2'	=> '已取消',
		'4'	=> '退货',
		'5'	=> '分发'
	);
}

/**
 * 
	01	在线支付
	02	工商银行
	03	建设银行
	04	招商银行
	05	交通一行
	06	农业银行
	07	广东发展银行
	08	兴业银行
	09	广大银行
	10	中信银行
	11	浦发银行
	12	中国银行
	13	深圳发展银行
	14	民生银行
	15	北京银行
	16	华夏银行
	17	中国邮政
	18	渤海银行
	19	平安银行
	10	南京银行
	21	手机支付
	22	汇付天下
	23	快钱
	24	嗖付
	25	财付通

 * 
 */
function get_payment_banks() {
	return array(
		'01'	=> '在线支付',
		'02'	=> '工商银行',
		'03'	=> '建设银行',
		'04'	=> '招商银行',
		'05'	=> '交通一行',
		'06'	=> '农业银行',
		'07'	=> '广东发展银行',
		'08'	=> '兴业银行',
		'09'	=> '广大银行',
		'10'	=> '中信银行',
		'11'	=> '浦发银行',
		'12'	=> '中国银行',
		'13'	=> '深圳发展银行',
		'14'	=> '民生银行',
		'15'	=> '北京银行',
		'16'	=> '华夏银行',
		'17'	=> '中国邮政',
		'18'	=> '渤海银行',
		'19'	=> '平安银行',
		'20'	=> '南京银行',
	);
}
function get_payment_online() {
	return array(
		'21'	=> '手机支付',
		'22'	=> '汇付天下',
		'23'	=> '快钱',
		'24'	=> '嗖付',
		'25'	=> '财付通',
	);
}
function get_payment_css_config() {
	return array(
//		'alipay'	=> '',
//		'balance'	=> '',	//余额支付
//		'bank'		=> '',	//银行转账
//		'cappay'	=> '',	//首信易支付
//		'chinabank'	=> '',	//网银在线 
//		'cncard'	=> '',	//云网支付
//		'cod'		=> '',	//货到付款
//		'express'	=> '',	//易捷IPS
//		'ips'		=> '',	//环迅IPS 
		'kuaiqian'	=> '21',	//快钱
		'kuaiqian_abc'	=> '06',
		'kuaiqian_bcom'	=> '05',
		'kuaiqian_bob'	=> '15',
		'kuaiqian_boc'	=> '12',
		'kuaiqian_ccb'	=> '03',
		'kuaiqian_cmb'	=> '04',
		'kuaiqian_cmbc'	=> '14',
		'kuaiqian_icbc'	=> '02',
		'kuaiqian_sdb'	=> '13',
//		'paypal'	=> '',	//paypal,
//		'paypal_ec'	=> '',	//paypal快速结帐 ,
		'post'		=> '17',
//		'shenzhou',
		'tenpay'	=> '25',
//		'tenpayc2c',
//		'yeepay',
//		'yeepayszx',
//		'yeepay_abchina',
//		'yeepay_ccb',
//		'yeepay_cmbchina',
//		'yeepay_icbc',
//		'yeepay_jcard',
	);
}

function get_goods_config() {
	return array(
		's'	=> '165/88',
		'm'	=> '170/92',
		'l'	=> '175/96',
		'xl'	=> '180/100',
		'xxl'	=> '185/104',
	);
}
function category_get_goods_attr($goodslist) {
	foreach($goodslist as &$item) {
        $properties = get_goods_properties($item["goods_id"]);  // 获得商品的规格和属性
		$item["taobao"] = @$properties["pro"]["URL"][GOODS_TAOBAO_URL_ATTRIBUTE_ID]["value"];
	}
	return $goodslist;
}
?>
